/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.peng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import me.peng.domain.RecyclerAddress;
import me.peng.domain.RecyclercdsRecord;
import me.peng.domain.config.SysResult;
import me.peng.domain.mapper.DsRecordMapper;
import me.peng.utils.ValidationUtil;
import me.peng.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.peng.repository.RecyclercdsRecordRepository;
import me.peng.service.RecyclercdsRecordService;
import me.peng.service.dto.RecyclercdsRecordDto;
import me.peng.service.dto.RecyclercdsRecordQueryCriteria;
import me.peng.service.mapstruct.RecyclercdsRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.peng.utils.PageUtil;
import me.peng.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author pengjianye
* @date 2021-09-30
**/
@Service
@RequiredArgsConstructor
public class RecyclercdsRecordServiceImpl implements RecyclercdsRecordService {

    private final RecyclercdsRecordRepository recyclercdsRecordRepository;
    private final RecyclercdsRecordMapper recyclercdsRecordMapper;

    @Autowired
    DsRecordMapper dsRecordMapper;

    @Override
    public Map<String,Object> queryAll(RecyclercdsRecordQueryCriteria criteria, Pageable pageable){
        Page<RecyclercdsRecord> page = recyclercdsRecordRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(recyclercdsRecordMapper::toDto));
    }

    @Override
    public List<RecyclercdsRecordDto> queryAll(RecyclercdsRecordQueryCriteria criteria){
        return recyclercdsRecordMapper.toDto(recyclercdsRecordRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public RecyclercdsRecordDto findById(Integer id) {
        RecyclercdsRecord recyclercdsRecord = recyclercdsRecordRepository.findById(id).orElseGet(RecyclercdsRecord::new);
        ValidationUtil.isNull(recyclercdsRecord.getId(),"RecyclercdsRecord","id",id);
        return recyclercdsRecordMapper.toDto(recyclercdsRecord);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RecyclercdsRecordDto create(RecyclercdsRecord resources) {
        return recyclercdsRecordMapper.toDto(recyclercdsRecordRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(RecyclercdsRecord resources) {
        RecyclercdsRecord recyclercdsRecord = recyclercdsRecordRepository.findById(resources.getId()).orElseGet(RecyclercdsRecord::new);
        ValidationUtil.isNull( recyclercdsRecord.getId(),"RecyclercdsRecord","id",resources.getId());
        recyclercdsRecord.copy(resources);
        recyclercdsRecordRepository.save(recyclercdsRecord);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            recyclercdsRecordRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<RecyclercdsRecordDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (RecyclercdsRecordDto recyclercdsRecord : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("用户名", recyclercdsRecord.getUname());
            map.put("账单标题", recyclercdsRecord.getRtitle());
            map.put("积分种类", recyclercdsRecord.getType());
            map.put("明细数字", recyclercdsRecord.getCoreditnum());
            map.put("创建日期", recyclercdsRecord.getCreatedate());
            map.put("用户id", recyclercdsRecord.getParentId());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public SysResult getCreditsRecord(Integer paretId) {

         QueryWrapper<RecyclercdsRecord> queryWrapper = new QueryWrapper<>();
        //selec * from RecyclercdsRecord where parent_id=pareiId
        queryWrapper.eq("parent_id", paretId);
        List<RecyclercdsRecord> list =   dsRecordMapper.selectList(queryWrapper);
        return SysResult.success(list);
    }

    /**
     * 提交积分记录
     * @param recyclercdsRecord
     * @return
     */
    @Override
    public SysResult postCreditsRecord(RecyclercdsRecord recyclercdsRecord) {

        dsRecordMapper.insert(recyclercdsRecord);

        return SysResult.success();
    }

}
